
*** Table 1 Summary Statistics 

file open Table using "$tables/Table1.tex", write replace

* table head
file write Table "\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} p{2.6in} r  r  r  r  r  r }" _n
file write Table "\toprule " _n
file write Table " & N & Mean        &  SD   & SD            & SD                    \\ " _n
file write Table " & &             &       & within        & within            \\ " _n
file write Table " & &             &       & NAICS6        & yr and NAICS6    \\ " _n
file write Table " \midrule " _n


* statisitcs:
foreach var of varlist ln_val_prod ln_val_imp ln_val_exp ln_val_con net_trade ln_wage twexb ///
	price_TIV price_TIV_ind price_foreign price_imports price_exports te_2007 ei ei_tot ci us_co2_rate_direct_combust {

	* extract variable label
	local label: variable label `var'
	* write the statistics for this variable
	
	* 1. mean & SD
	quietly sum `var' [w=wgt_prod_07]
	file write Table "`label' & " _tab %7.0f (r(N)) " &" _tab %7.3f (r(mean)) " &" _tab %7.3f (r(sd)) " &"

	if ("`var'"!="ei" & "`var'"!="ei_tot" & "`var'"!="ei_2" & "`var'"!="ci" & "`var'"!="te_2007" & "`var'"!="us_co2_rate_direct_combust") {
		* 2. SD within NAICS3
		quietly reghdfe `var' [w=wgt_prod_07], absorb(naics) residuals(withinNAICS6)
		quietly sum withinNAICS6 [w=wgt_prod_07]
		file write Table  _tab %7.3f (r(sd)) " &"
		drop withinNAICS6
		
		* 3. SD within yr*NAICS3
		quietly reghdfe `var' [w=wgt_prod_07], absorb(i.naics i.year) residuals(withinNAICS6yr)
		quietly sum withinNAICS6yr [w=wgt_prod_07]
		file write Table  _tab %7.3f (r(sd)) " "
		drop withinNAICS6yr
	} 
	if ("`var'"=="twexb" | "`var'"=="price_exports") {
		file write Table  " \\ \midrule " _n
	}
	else if ("`var'"=="us_co2_rate_direct_combust") {
		file write Table  " " _n
	}
	else {
		file write Table  " \\[0.5em] " _n
	}
	
}
*

* end of table
file write Table "\\ \bottomrule " _n
file write Table "\end{tabular*} " _n
file write Table "\vspace{-.5ex} " _n

* footnote
file write Table "\begin{minipage}{\textwidth} " _n
file write Table "{\footnotesize \textit{Notes:} All values deflated to 2007 U.S. dollars. Variables weighted by shipment values in 2007. Measures of energy intensity and trade exposure are held fixed at their 2007 values. The data covers 312 manufacturing NAICS6 sectors over twenty years (1996-2015). A few sectors are missing BLS wages for a few years thus leading to a few missing values that get transmitted to energy prices and carbon intensity. A few sectors have negative consumption, thus also leading to a few missing values in the log of consumption.} "_n
file write Table "\end{minipage} " _n


* close file
file close Table
